#!/bin/bash

Get_Dist_Name()
{
    DISTRO=''
    PM=''
    CodeName=''
    Debian_Ver=''
    CentOS_Ver=''
    if grep -Eqi "CentOS" /etc/issue || grep -Eq "CentOS" /etc/*-release; then
        DISTRO='CentOS'
        PM='yum'
        if grep -Eqi "release 5." /etc/redhat-release; then
            CentOS_Ver='5'
         elif grep -Eqi "release 6." /etc/redhat-release; then
            CentOS_Ver='6'
         elif grep -Eqi "release 7." /etc/redhat-release; then
            CentOS_Ver='7'
            firewall-cmd --zone=public --add-port=80/tcp --permanent
            firewall-cmd --reload
         else
            CentOS_Ver='8'
            firewall-cmd --zone=public --add-port=80/tcp --permanent
            firewall-cmd --reload
        fi
     elif grep -Eqi "Debian" /etc/issue || grep -Eq "Debian" /etc/*-release; then
        DISTRO='Debian'
        PM='apt'
        if grep -Eqi "6." /etc/debian_version ; then
            CodeName='squeeze'
            Debian_Ver='6'
         elif grep -Eqi "7." /etc/debian_version ; then
            CodeName='wheezy'
            Debian_Ver='7'
         elif grep -Eqi "8." /etc/debian_version ; then
            CodeName='jessie'
            Debian_Ver='8'
         elif grep -Eqi "9." /etc/debian_version ; then
            CodeName='stretch'
            Debian_Ver='9'
         else
            CodeName='buster'
            Debian_Ver='10'
        fi
     else
        DISTRO='unknow'
    fi
    if [[ `getconf WORD_BIT` = '32' && `getconf LONG_BIT` = '64' ]] ; then
        Is_64bit='y'
     else
        Is_64bit='n'
    fi
}

Set_Iptables()
{
    if [ -s /sbin/iptables ]; then
        /sbin/iptables -I INPUT 1 -i lo -j ACCEPT
        /sbin/iptables -I INPUT 2 -m state --state ESTABLISHED,RELATED -j ACCEPT
        /sbin/iptables -I INPUT 3 -p tcp --dport 80 -j ACCEPT
        /sbin/iptables -I INPUT 4 -p tcp -s 127.0.0.1 --dport 3306 -j ACCEPT
        /sbin/iptables -I INPUT 5 -p tcp --dport 3306 -j DROP
        if [ "$PM" = "yum" ]; then
            service iptables save
         elif [ "$PM" = "apt" ]; then
            iptables-save > /etc/iptables.rules
            cat >/etc/network/if-post-down.d/iptables<<EOF
#!/bin/bash
iptables-save > /etc/iptables.rules
EOF
            chmod +x /etc/network/if-post-down.d/iptables
            cat >/etc/network/if-pre-up.d/iptables<<EOF
#!/bin/bash
iptables-restore < /etc/iptables.rules
EOF
            chmod +x /etc/network/if-pre-up.d/iptables
        fi
    fi
}

Print_APP_Ver()
{
    echo ""
    echo -e "\033[32m You will install ... \033[0m"
    echo "    ${Nginx_Ver}${Nginx_Info}"
    echo "    ${MariaDB_Ver}"
    echo "    ${PHP_Ver}"
    echo "Print moodle.conf infomation..."
    echo "    Download Mirror: ${Download_Mirror}"
    echo "    Database Root Password: ${DB_Root_Password}"
    echo "    Database Directory: ${DB_Data_Dir}"
    echo "    Default Moodle Directory: ${Default_Moodle_Dir}"
    echo "    Moodle admin Password: ${Admin_Password}"
}

Print_Sys_Info()
{
    echo "System is: ${DISTRO} ${Debian_Ver}${CentOS_Ver}"
    MemTotal=`free -m | grep Mem | awk '{print  $2}'`
    echo "Memory is: ${MemTotal} MB "
    if [ "$PM" = "yum" ]; then
        Network_Config=$( ls /etc/sysconfig/network-scripts/ | grep ifcfg-eno )
        IP=$( cat /etc/sysconfig/network-scripts/${Network_Config} | grep IPADDR | awk -F\" '{print $2}' )
     elif [ "$PM" = "apt" ]; then
        IP=$( cat /etc/network/interfaces | grep address | awk -F" " '{print $2}' | awk -F "/" '{print $1}' )
    fi
    echo "IP is: ${IP}"
}


Press_Install()
{
    echo ""
    echo -e "\033[35mPress any key to install...or Press Ctrl+c to cancel\033[0m"
    OLDCONFIG=`stty -g`
    stty -icanon -echo min 1 time 0
    dd count=1 2>/dev/null
    stty ${OLDCONFIG}
    if ps aux | grep "yum" | grep -qv "grep"; then
        killall yum
     elif ps aux | grep "apt-get" | grep -qv "grep"; then
        killall apt-get
    fi
}

Check_Hosts()
{
    if grep -Eqi '^127.0.0.1[[:space:]]*localhost' /etc/hosts; then
        echo "Hosts: ok."
     else
        echo "127.0.0.1 localhost.localdomain localhost" >> /etc/hosts
    fi
}


CentOS_Modify_Source()
{
    \cp ${cur_dir}/conf/CentOS-Base-tuna.repo /etc/yum.repos.d/CentOS-Base.repo
    sed -i "s/\$releasever/${CentOS_Ver}/g" /etc/yum.repos.d/CentOS-Base.repo
    sed -i "s/RPM-GPG-KEY-CentOS-7/RPM-GPG-KEY-CentOS-${CentOS_Ver}/g" /etc/yum.repos.d/CentOS-Base.repo
    yum clean all
    yum makecache
}

Debian_Modify_Source()
{
    if [ "${CodeName}" != "" ]; then
        \cp /etc/apt/sources.list /etc/apt/sources.list.$(date +"%Y%m%d")
        cat > /etc/apt/sources.list<<EOF
deb http://mirrors.ustc.edu.cn/debian/ ${CodeName} main contrib non-free
deb http://mirrors.ustc.edu.cn/debian/ ${CodeName}-updates main contrib non-free
deb http://mirrors.ustc.edu.cn/debian-security/ ${CodeName}/updates main contrib non-free
EOF
    fi
}

Set_Timezone()
{
    echo -e "\033[34m Setting timezone... \033[0m"
    rm -rf /etc/localtime
    ln -s /usr/share/zoneinfo/Asia/Shanghai /etc/localtime
}

Deb_InstallNTP()
{
    echo -e "\033[34m [+] Installing ntp... \033[0m"
    apt-get update -y
    apt-get install -y ntpdate
    ntpdate -u pool.ntp.org
    date
}

CentOS_InstallNTP()
{
    echo -e "\033[34m [+] Installing ntp... \033[0m"
    yum install -y ntp
    ntpdate -u pool.ntp.org
    date
}

Deb_RemoveAMP()
{
    echo -e "\033[34m [-] apt-get remove packages... \033[0m"
    apt-get update -y
    for removepackages in apache2 apache2-doc apache2-utils apache2.2-common apache2.2-bin apache2-mpm-prefork apache2-doc apache2-mpm-worker mysql-client mysql-server mysql-common mysql-server-core-5.5 mysql-client-5.5 php5 php5-common php5-cgi php5-cli php5-mysql php5-curl php5-gd;
        do apt-get purge -y $removepackages; done
    killall apache2
    dpkg -l |grep apache 
    dpkg -P apache2 apache2-doc apache2-mpm-prefork apache2-utils apache2.2-common
    dpkg -l |grep mysql 
    dpkg -P mysql-server mysql-common libmysqlclient15off libmysqlclient15-dev
    dpkg -l |grep php
    dpkg -P php5 php5-common php5-cli php5-cgi php5-mysql php5-curl php5-gd
    apt-get autoremove -y && apt-get clean
    rm -f /etc/my.cnf
    rm -rf /etc/mysql/
}

CentOS_RemoveAMP()
{
    echo -e "\033[34m [-] Yum remove packages... \033[0m"
    rpm -qa|grep httpd
    rpm -e httpd httpd-tools
    rpm -qa|grep mysql
    rpm -e mysql mysql-libs
    rpm -qa|grep php
    rpm -e php-mysql php-cli php-gd php-common php
    yum -y remove httpd*
    yum -y remove mysql-server mysql mysql-libs
    yum -y remove php*
    yum clean all
}

Deb_Dependent()
{
    echo -e "\033[34m [-] Apt-get installing dependent packages.... \033[0m"
    apt-get update -y
    apt-get autoremove -y
    apt-get -fy install
    export DEBIAN_FRONTEND=noninteractive
    apt-get --no-install-recommends install -y build-essential gcc g++ make
    for packages in debian-keyring debian-archive-keyring build-essential gcc g++ make cmake autoconf automake re2c wget cron bzip2 libzip-dev libc6-dev bison file rcconf flex vim bison m4 gawk less cpp binutils diffutils unzip tar bzip2 libbz2-dev libncurses5 libncurses5-dev libtool libevent-dev openssl libssl-dev zlibc libsasl2-dev libltdl3-dev libltdl-dev zlib1g zlib1g-dev libbz2-1.0 libbz2-dev libglib2.0-0 libglib2.0-dev libpng3 libjpeg-dev libpng-dev libpng12-0 libpng12-dev libkrb5-dev curl libcurl3-gnutls libcurl4-gnutls-dev libcurl4-openssl-dev libpq-dev libpq5 gettext libpng12-dev libxml2-dev libcap-dev ca-certificates libc-client2007e-dev psmisc patch git libc-ares-dev libicu-dev e2fsprogs libxslt libxslt1-dev libc-client-dev xz-utils libexpat1-dev libaio-dev libtirpc-dev;
        do apt-get --no-install-recommends install -y $packages --force-yes; done
}

CentOS_Dependent()
{
    echo -e "\033[34m [-] Yum installing dependent packages... \033[0m"
    cp /etc/yum.conf /etc/yum.conf.lnmp
    sed -i 's:exclude=.*:exclude=:g' /etc/yum.conf
    for packages in make cmake gcc gcc-c++ gcc-g77 flex bison file libtool libtool-libs autoconf kernel-devel patch wget crontabs libjpeg libjpeg-devel libpng libpng-devel libpng10 libpng10-devel gd gd-devel libxml2 libxml2-devel zlib zlib-devel glib2 glib2-devel unzip tar bzip2 bzip2-devel libzip-devel libevent libevent-devel ncurses ncurses-devel curl curl-devel libcurl libcurl-devel e2fsprogs e2fsprogs-devel krb5 krb5-devel libidn libidn-devel openssl openssl-devel vim-minimal gettext gettext-devel ncurses-devel gmp-devel pspell-devel unzip libcap diffutils ca-certificates net-tools libc-client-devel psmisc libXpm-devel git-core c-ares-devel libicu-devel libxslt libxslt-devel xz expat-devel libaio-devel rpcgen libtirpc-devel perl;
        do yum -y install $packages; done
    mv -f /etc/yum.conf.lnmp /etc/yum.conf
}

Disable_Selinux()
{
    if [ -s /etc/selinux/config ]; then
        sed -i 's/SELINUX=enforcing/SELINUX=disabled/g' /etc/selinux/config
    fi
}

Check_Mirror()
{
    if [ "$PM" = "yum" ]; then
        yum install -y curl
    elif [ "$PM" = "apt" ]; then
        apt-get update
        apt-get install -y curl
    fi
    if [ "${Download_Mirror}" = "http://hongm.emoodle.org/down" ]; then
        mirror_code=`curl -o /dev/null -m 20 --connect-timeout 20 -sk -w %{http_code} http://hongm.emoodle.org/down/`
        if [ "${mirror_code}" != "200" ]; then
            mirror_code=`curl -o /dev/null -m 20 --connect-timeout 20 -sk -w %{http_code} https://study.wjszzx.com.cn/soft/`
            if [ "${mirror_code}" = "200" ]; then
                echo "Change to mirror https://study.wjszzx.com.cn/soft"
                Download_Mirror='https://study.wjszzx.com.cn/soft'
            else
                echo "Dwonload Server Error!"
                exit 1
            fi
        fi
    fi
}

Check_Download()
{
    echo -e "   \e[0;31m[+] Downloading files...\e[0m"
    mkdir -p ${cur_dir}/src
    cd ${cur_dir}/src
    wget -c --progress=bar:force --prefer-family=IPv4 --no-check-certificate ${Download_Mirror}/nginx/${Nginx_Ver}.tar.gz
        [[ ! -s ${Nginx_Ver}.tar.gz ]] && wget -c --progress=bar:force --prefer-family=IPv4 --no-check-certificate http://nginx.org/download/${Nginx_Ver}.tar.gz
    wget -c --progress=bar:force --prefer-family=IPv4 --no-check-certificate ${Download_Mirror}/mariadb/${MariaDB_Ver}.tar.gz
        [[ ! -s ${MariaDB_Ver}.tar.gz ]] && wget -c --progress=bar:force --prefer-family=IPv4 --no-check-certificate https://mirrors.tuna.tsinghua.edu.cn/mariadb/${MariaDB_Ver}/source/${MariaDB_Ver}.tar.gz --no-check-certificate
    wget -c --progress=bar:force --prefer-family=IPv4 --no-check-certificate ${Download_Mirror}/php/${PHP_Ver}.tar.bz2
        [[ ! -s ${PHP_Ver}.tar.bz2 ]] && wget -c --progress=bar:force --prefer-family=IPv4 --no-check-certificate http://cn2.php.net/distributions/${PHP_Ver}.tar.bz2
    wget -c --progress=bar:force --prefer-family=IPv4 --no-check-certificate ${Download_Mirror}/autoconf/${Autoconf_Ver}.tar.gz
        [[ ! -s ${Autoconf_Ver}.tar.gz ]] && wget -c --progress=bar:force --prefer-family=IPv4 --no-check-certificate https://ftp.gnu.org/gnu/autoconf/${Autoconf_Ver}.tar.gz
    wget -c --progress=bar:force --prefer-family=IPv4 --no-check-certificate ${Download_Mirror}/cmake/${Cmake_Ver}.tar.gz
        [[ ! -s ${Cmake_Ver}.tar.gz ]] && wget -c --progress=bar:force --prefer-family=IPv4 --no-check-certificate https://cmake.org/files/v$(echo ${Cmake_Ver} | awk -F[-.] '{print $2}').$(echo ${Cmake_Ver} | awk -F[-.] '{print $3}')/${Cmake_Ver}.tar.gz --no-check-certificate
    wget -c --progress=bar:force --prefer-family=IPv4 --no-check-certificate ${Download_Mirror}/libiconv/${Libiconv_Ver}.tar.gz
        [[ ! -s ${Libiconv_Ver}.tar.gz ]] && wget -c --progress=bar:force --prefer-family=IPv4 --no-check-certificate http://ftp.gnu.org/gnu/libiconv/${Libiconv_Ver}.tar.gz
    wget -c --progress=bar:force --prefer-family=IPv4 --no-check-certificate ${Download_Mirror}/mhash/${Mhash_Ver}.tar.bz2
        [[ ! -s ${Mhash_Ver}.tar.bz2 ]] && wget -c --progress=bar:force --prefer-family=IPv4 --no-check-certificate https://sourceforge.net/projects/mhash/files/mhash/$(echo ${Mhash_Ver} | awk -F"-" '{print $2}')/${Mhash_Ver}.tar.bz2
    wget -c --progress=bar:force --prefer-family=IPv4 --no-check-certificate ${Download_Mirror}/freetype/${Freetype_Ver}.tar.bz2
        [[ ! -s ${Freetype_Ver}.tar.bz2 ]] && wget -c --progress=bar:force --prefer-family=IPv4 --no-check-certificate http://download.savannah.gnu.org/releases/freetype/${Freetype_Ver}.tar.bz2
    wget -c --progress=bar:force --prefer-family=IPv4 --no-check-certificate ${Download_Mirror}/curl/${Curl_Ver}.tar.bz2
        [[ ! -s ${Curl_Ver}.tar.bz2 ]] && wget -c --progress=bar:force --prefer-family=IPv4 --no-check-certificate https://curl.haxx.se/download/${Curl_Ver}.tar.bz2
    wget -c --progress=bar:force --prefer-family=IPv4 --no-check-certificate ${Download_Mirror}/pcre/${Pcre_Ver}.tar.bz2
        [[ ! -s ${Pcre_Ver}.tar.bz2 ]] && wget -c --progress=bar:force --prefer-family=IPv4 --no-check-certificate ftp://ftp.csx.cam.ac.uk/pub/software/programming/pcre/${Pcre_Ver}.tar.bz2
}

Install_Autoconf()
{
    echo -e "   \e[0;31m[+] Installing ${Autoconf_Ver}\e[0m"
    cd ${cur_dir}/src
    tar zxf ${Autoconf_Ver}.tar.gz
    cd ${Autoconf_Ver}
    ./configure --prefix=/usr/local/${Autoconf_Ver}
    make && make install
    cd ${cur_dir}/src/
    rm -rf ${cur_dir}/src/${Autoconf_Ver}
    export PHP_AUTOCONF=/usr/local/${Autoconf_Ver}/bin/autoconf
    export PHP_AUTOHEADER=/usr/local/${Autoconf_Ver}/bin/autoheader
}

Install_Cmake()
{
    echo -e "   \e[0;31m[+] Installing ${Cmake_Ver}\e[0m"
    cd ${cur_dir}/src
    tar zxf ${Cmake_Ver}.tar.gz
    cd ${Cmake_Ver}
    ./configure
    make && make install
    ldconfig
    rm -fr ${cur_dir}/src/${Cmake_Ver}
}

Install_Libiconv()
{
    echo -e "   \e[0;31m[+] Installing ${Libiconv_Ver}\e[0m"
    cd ${cur_dir}/src
    tar zxf ${Libiconv_Ver}.tar.gz
    cd ${Libiconv_Ver}
    patch -p0 < ${cur_dir}/conf/libiconv-glibc-2.16.patch
    ./configure --enable-static
    make && make install
    ldconfig
    rm -fr ${cur_dir}/src/${Libiconv_Ver}
}

Install_Libtidy()
{
    echo -e "   \e[0;31m[+] Installing ${Libtidy_Ver}\e[0m"
    cd ${cur_dir}/src
    [[ ! -s ${Libtidy_Ver}.tar.gz ]] && wget -c --progress=bar:force --prefer-family=IPv4 --no-check-certificate ${Download_Mirror}/tidy/${Libtidy_Ver}.tar.gz
    tar zxf ${Libtidy_Ver}.tar.gz
    cd ${Libtidy_Ver}
    ./configure
    make && make install
    ldconfig
    rm -fr ${cur_dir}/src/${Libtidy_Ver}
}

Install_Mhash()
{
    echo -e "   \e[0;31m[+] Installing ${Mhash_Ver}\e[0m"
    cd ${cur_dir}/src
    tar jxf ${Mhash_Ver}.tar.bz2
    cd ${Mhash_Ver}
    ./configure
    make && make install
    ln -s /usr/local/lib/libmhash.a /usr/lib/libmhash.a
    ln -s /usr/local/lib/libmhash.la /usr/lib/libmhash.la
    ln -s /usr/local/lib/libmhash.so /usr/lib/libmhash.so
    ln -s /usr/local/lib/libmhash.so.2 /usr/lib/libmhash.so.2
    ln -s /usr/local/lib/libmhash.so.2.0.1 /usr/lib/libmhash.so.2.0.1
    ldconfig
    rm -fr ${cur_dir}/src/${Mhash_Ver}
}

Install_Freetype()
{
    echo -e "   \e[0;31m[+] Installing ${Freetype_Ver}\e[0m"
    cd ${cur_dir}/src
    tar jxf ${Freetype_Ver}.tar.bz2
    cd ${Freetype_Ver}
    ./configure --prefix=/usr/local/freetype
    make && make install
    ldconfig
    rm -fr ${cur_dir}/src/${Freetype_Ver}
}

Install_Icu4c()
{
    if [ ! -s /usr/bin/icu-config ] || /usr/bin/icu-config --version | grep '^3.'; then
        echo -e "   \e[0;31m[+] Installing ${Libicu4c_Ver}\e[0m"
        cd ${cur_dir}/src
        wget -c --progress=bar:force --prefer-family=IPv4 --no-check-certificate ${Download_Mirror}/icu4c/${Libicu4c_Ver}-src.tgz
        tar zxf ${Libicu4c_Ver}-src.tgz
        cd icu/source
        ./configure
        make && make install
        ldconfig
        rm -fr ${cur_dir}/src/icu
    fi
}

Install_Curl()
{
    if [[ ! -s /usr/local/curl/bin/curl || ! -s /usr/local/curl/lib/libcurl.so || ! -s /usr/local/curl/include/curl/curl.h ]]; then
        echo -e "   \e[0;31m[+] Installing ${Curl_Ver}\e[0m"
        cd ${cur_dir}/src
        tar jxf ${Curl_Ver}.tar.bz2
        cd ${Curl_Ver}
        ./configure --prefix=/usr/local/curl --enable-ares --without-nss --with-ssl
        make && make install
        ldconfig
        rm -fr ${cur_dir}/src/${Curl_Ver}
    fi
    if [ -s /usr/local/lib/libcurl.so ]; then
        rm -f /usr/local/lib/libcurl*
    fi
}

Install_Pcre()
{
    if [ ! -s /usr/bin/pcre-config ] || /usr/bin/pcre-config --version | grep -vEqi '^8.'; then
        echo -e "   \e[0;31m[+] Installing ${Pcre_Ver}\e[0m"
        cd ${cur_dir}/src
        tar jxf ${Pcre_Ver}.tar.bz2
        cd ${Pcre_Ver}
        ./configure
        make && make install
        ldconfig
        rm -fr ${cur_dir}/src/${Pcre_Ver}
    fi
}

Install_Boost()
{
    echo -e "   \e[0;31m[+] Installing ${Boost_Ver}\e[0m"
    cd ${cur_dir}/src
    wget -c --progress=bar:force --prefer-family=IPv4 --no-check-certificate ${Download_Mirror}/boost/${Boost_Ver}.tar.bz2
    tar jxf ${Pcre_Ver}.tar.bz2
    cd ${Pcre_Ver}
    ./bootstrap.sh
    ./b2
    ./b2 install
    cd ${cur_dir}/src/
    rm -rf ${cur_dir}/src/${Boost_Ver}
}

Install_TCMalloc()
{
    echo -e "   \e[0;31m[+] Installing ${TCMalloc_Ver}\e[0m"
    if [ "${Is_64bit}" = "y" ] ; then
        cd ${cur_dir}/src
        wget -c --progress=bar:force --prefer-family=IPv4 --no-check-certificate ${Download_Mirror}/libunwind/${Libunwind_Ver}.tar.gz
        tar zxf ${Libunwind_Ver}.tar.gz
        cd ${Libunwind_Ver}
        CFLAGS=-fPIC ./configure
        make CFLAGS=-fPIC
        make CFLAGS=-fPIC install
        rm -rf ${cur_dir}/src/${Libunwind_Ver}
    fi
    cd ${cur_dir}/src
    wget -c --progress=bar:force --prefer-family=IPv4 --no-check-certificate ${Download_Mirror}/tcmalloc/${TCMalloc_Ver}.tar.gz
    tar zxf ${TCMalloc_Ver}.tar.gz
    cd ${TCMalloc_Ver}
    if [ "${Is_64bit}" = "y" ] ; then
        ./configure
     else
        ./configure --enable-frame-pointers
    fi
    make && make install
    ldconfig
    rm -rf ${cur_dir}/src/${TCMalloc_Ver}
}

CentOS_Lib_Opt()
{
    if [ "${Is_64bit}" = "y" ] ; then
        ln -s /usr/lib64/libpng.* /usr/lib/
        ln -s /usr/lib64/libjpeg.* /usr/lib/
    fi
    ulimit -v unlimited
    if [ `grep -L "/lib"    '/etc/ld.so.conf'` ]; then
        echo "/lib" >> /etc/ld.so.conf
    fi
    if [ `grep -L '/usr/lib'    '/etc/ld.so.conf'` ]; then
        echo "/usr/lib" >> /etc/ld.so.conf
    fi
    if [ -d "/usr/lib64" ] && [ `grep -L '/usr/lib64'    '/etc/ld.so.conf'` ]; then
        echo "/usr/lib64" >> /etc/ld.so.conf
    fi
    if [ `grep -L '/usr/local/lib'    '/etc/ld.so.conf'` ]; then
        echo "/usr/local/lib" >> /etc/ld.so.conf
    fi
    ldconfig
    if ! cat /etc/security/limits.conf | grep -Eqi "soft nproc 65535" ; then
        cat >>/etc/security/limits.conf<<eof
* soft nproc 65535
* hard nproc 65535
* soft nofile 65535
* hard nofile 65535
eof
    fi
    if ! cat /etc/sysctl.conf | grep -Eqi fs.file-max=65535 ; then
        echo "fs.file-max=65535" >> /etc/sysctl.conf
    fi
    if ! cat /etc/sysctl.conf | grep vm.nr_hugepages ; then
        echo "vm.nr_hugepages=64" >> /etc/sysctl.conf
    fi
    sysctl vm.nr_hugepages=64
}

Deb_Lib_Opt()
{
    if [ "${Is_64bit}" = "y" ] ; then
        ln -s /usr/lib/x86_64-linux-gnu/libpng* /usr/lib/
        ln -s /usr/lib/x86_64-linux-gnu/libjpeg* /usr/lib/
        ln -s /usr/lib/x86_64-linux-gnu/libssl* /usr/lib/
      else
        ln -s /usr/lib/i386-linux-gnu/libpng* /usr/lib/
        ln -s /usr/lib/i386-linux-gnu/libjpeg* /usr/lib/
        ln -s /usr/lib/i386-linux-gnu/libssl* /usr/lib/
        ln -s /usr/include/i386-linux-gnu/asm /usr/include/asm
    fi
    ulimit -v unlimited
    if [ `grep -L "/lib"    '/etc/ld.so.conf'` ]; then
        echo "/lib" >> /etc/ld.so.conf
    fi
    if [ `grep -L '/usr/lib'    '/etc/ld.so.conf'` ]; then
        echo "/usr/lib" >> /etc/ld.so.conf
    fi
    if [ -d "/usr/lib64" ] && [ `grep -L '/usr/lib64'    '/etc/ld.so.conf'` ]; then
        echo "/usr/lib64" >> /etc/ld.so.conf
    fi
    if [ `grep -L '/usr/local/lib'    '/etc/ld.so.conf'` ]; then
        echo "/usr/local/lib" >> /etc/ld.so.conf
    fi
    ldconfig
    if ! cat /etc/security/limits.conf | grep -Eqi "soft nproc 65535" ; then
        cat >>/etc/security/limits.conf<<eof
* soft nproc 65535
* hard nproc 65535
* soft nofile 65535
* hard nofile 65535
eof
    fi
    if ! cat /etc/sysctl.conf | grep -Eqi fs.file-max=65535 ; then
        echo "fs.file-max=65535" >> /etc/sysctl.conf
    fi
    if ! cat /etc/sysctl.conf | grep vm.nr_hugepages ; then
        echo "vm.nr_hugepages=64" >> /etc/sysctl.conf
    fi
    sysctl vm.nr_hugepages=64
}

Press_Start()
{
    echo ""
    echo -e "\033[35mPress any key to start upgrade... or Press Ctrl+c to cancel\033[0m"
    OLDCONFIG=`stty -g`
    stty -icanon -echo min 1 time 0
    dd count=1 2>/dev/null
    stty ${OLDCONFIG}
}

Verify_DB_Password()
{
    MySQL_Bin="/usr/local/mariadb/bin/mysql"
    MySQL_Config="/usr/local/mariadb/bin/mysql_config"
    MySQL_Dir="/usr/local/mariadb"
    status=1
    while [ $status -eq 1 ]; do
        stty -echo
        echo "Enter current root password of Database (Password will not shown): "
        read DB_Root_Password
        echo
        stty echo
        cat >~/.my.cnf<<EOF
[client]
user=root
password='${DB_Root_Password}'
EOF
        echo "" >/tmp/.mysql.tmp
        ${MySQL_Bin} --defaults-file=~/.my.cnf </tmp/.mysql.tmp
        status=$?
    done
    echo "OK, MySQL root password correct."
}
